/**
 * Created by CFlower on 2017/11/7.
 */
$(function() {
    $('#dataGrid').datagrid({
        url : 'machining/dataGrid',
        striped : true,
        rownumbers : true,
        pagination : true,
        singleSelect : true,
        idField : 'id',
        sortName : 'id',
        sortOrder : 'asc',
        pageSize : 50,
        pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
        frozenColumns : [ [ {
            width : '180',
            title : '工况名称',
            field : 'machiningName'
        },{
            field : 'action',
            title : '操作',
            width : 200,
            formatter : function(value, row, index) {
                var str = '&nbsp;';
                if(row.isdefault!=0){
                    if ($.canEdit) {
                        str += $.formatString('<a href="javascript:void(0)" onclick="editMachining(\'{0}\');" >编辑</a>', index);
                    }
                    if ($.canDelete) {
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" onclick="deleteMachining(\'{0}\');" >删除</a>', row.id);
                    }
                }
                return str;
            }
        } ] ],
        toolbar : '#toolbar'
    });

    $("#dia-add").dialog({
        title:"添加工况",
        modal: true,
        width: 500,
        height: 300,
        closed: true,
        buttons : [{
            text : '添加',
            iconCls : 'icon-add',
            handler : function () {
                var bool=$("#form-add").form('validate');
                if(bool){
                    $.ajax({
                        cache:false,
                        url:'machining/add',
                        data:$("#form-add").serialize(),
                        type:"POST",
                        dataType:"json",
                        success:function(result){
                            if(!result.success){
                                alert(result.msg);
                            }else{
                                $("#dataGrid").datagrid('reload');
                                $("#dia-add").dialog('close');
                            }
                        }
                    });
                }
            }
        },{
            text : '取消',
            iconCls : 'icon-redo',
            handler : function () {
                $("#dia-add").dialog('close');
            }
        }]
    });

    $("#dia-edit").dialog({
        title:"编辑工况",
        modal: true,
        width: 550,
        height: 350,
        closed: true,
        buttons : [{
            text : '编辑',
            iconCls : 'icon-edit',
            handler : function () {
                var bool=$("#form-edit").form('validate');
                if(bool){
                    $.ajax({
                        cache:false,
                        url:'machining/edit',
                        data:$("#form-edit").serialize(),
                        type:"POST",
                        dataType:"json",
                        success:function(map){
                            if(!map.success){
                                alert(map.message);
                            }else{
                                $('#dataGrid').datagrid('reload');
                                $("#dia-edit").dialog('close');
                            }
                        }
                    });
                }
            }
        },{
            text : '取消',
            iconCls : 'icon-redo',
            handler : function () {
                $("#dia-edit").dialog('close');
            }
        }]
    });
});

function addMachining() {
    $("#dia-add").dialog('open');
}


function editMachining(index) {
    var rows=$('#dataGrid').datagrid('getRows');
    var row=rows[index];
    $("#dia-edit input[name=id]").val(row.id);
    $("#dia-edit input[name=machiningName]").val(row.machiningName);

    $("#dia-edit").dialog('open');

}

function deleteMachining(id) {
    if(confirm("确定要删除当前工况吗？")){
        $.ajax({
            cache:false,
            url:"machining/delete",
            data:{id:id},
            type:"POST",
            dataType:"json",
            success:function(result) {
                if(result.success){
                    $('#dataGrid').datagrid('reload');
                }else{
                    alert(result.message);
                }
            }
        });
    }
}